
package editor.map.dataOutput
{
	import editor.common.Common;
	import editor.map.elements.AvatarNpc;
	import editor.map.elements.Npc;
	import editor.map.elements.Teleport;
	import editor.map.minimap.Minimap;
	import editor.ui.Alert;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.core.Singleton;
	
	/**
	 * @author 缺硒
	 */
	public class MapdataInputer
	{
		private static var __instance:MapdataInputer;
		
		private var _mapId:String;
		private var _path:String;
		private var _mapXml:XML;
		private var _tileData:Array;
		private var _bgBmd:BitmapData;
		
		
		public static function getInstance():MapdataInputer
		{
			if(__instance == null) __instance = new MapdataInputer(new Singleton());
			return __instance;
		}
		
		public function MapdataInputer(s:Singleton)
		{
			
		}
		
		/**
		 * 打开地图信息
		 * @param mapId
		 * @param path 
		 */		
		public function readMapData(mapId:String, path:String):void
		{
			Alert.showLog("loading ...");
			_mapId = mapId;
			_path = path;
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onMapXmlOver);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onCfgError);
			loader.load(new URLRequest(_path + "/" + _mapId + ".xml"));
		}
		
		//xml加载完成
		private function onMapXmlOver(e:Event):void
		{
			_mapXml = new XML(e.target.data);
			trace(_mapXml)
			//加载tile
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMaptileOver);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onDataError);
			loader.load(new URLRequest(_path + "/res/path.png"));
		}
		
		//tile加载完成
		private function onMaptileOver(e:Event):void
		{
			var loader:Loader = e.target.loader;
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onMaptileOver);
			var bmp:BitmapData = Bitmap(loader.content).bitmapData;
			_tileData = MapdataOutputer.readTileBitmapdata(bmp);
			
			loadBg();
		}
		
		//加载背景
		private function loadBg():void
		{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMapBgOver);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onBgError);
			loader.load(new URLRequest(_path + "/" + _mapId + "_bg.png"));
			
		}
		
		
		//背景加载完成
		private function onMapBgOver(e:Event):void
		{
			var loader:Loader = e.target.loader;
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onMapBgOver);
			var bmd:BitmapData = Bitmap(loader.content).bitmapData;
			_bgBmd = bmd;
			initMapEditor();
		}
		
		//初始化编辑器
		private function initMapEditor():void
		{
			Common.map.backgroundLayer.bgFile = new File().resolvePath(_path + "/" + _mapId + "_bg.png");
			var dfile:File = new File().resolvePath(_path + "/res/distant.jpg");
			//判断是否已保存文件存在远景图
			if(dfile.exists) 
			{
				Common.ui.outputWindow.isDistant = true;
				Common.ui.outputWindow.distantFile = dfile;
			}
			else
			{
				Common.ui.outputWindow.isDistant = false;
				Common.ui.outputWindow.distantFile = null;
			}
			Common.map.backgroundLayer.addBitmapdata(_bgBmd);
			Common.map.tileLayer.inputTileData(_tileData);
			praseMapXml();
			_bgBmd = null;
			_tileData = null;
			
			Alert.hideLog();
		}
		
		private function praseMapXml():void
		{
			var id:String = _mapXml.scene.@id;
			var mapName:String = _mapXml.scene.name;
			var npcList:Vector.<AvatarNpc> = new Vector.<AvatarNpc>();
			var npcXml:XMLList = _mapXml.scene.npc;
			
			for (var i:int = 0; i < npcXml.length(); i ++)
			{
				var npc:AvatarNpc = new AvatarNpc(npcXml[i].@id,npcXml[i].@x,npcXml[i].@y);
				
				for(var j:int = 0; j < npcXml[i].point.length(); j ++)
				{
					npc.standPoints[j].x = npcXml[i].point[j].@x * 16 - npc.x;
					npc.standPoints[j].y = npcXml[i].point[j].@y * 16 - npc.y;
				}
				npcList.push(npc);
			}
			
			var gateXml:XMLList = _mapXml.scene.gate;
			var inGate:Teleport;
			var preGate:Teleport;
			var nextGate:Teleport;
			
			for (i = 0; i < gateXml.length(); i ++)
			{
				if(gateXml[i].@kind == "in")
				{
					inGate	= new Teleport(Minimap.MINI_IN_COLOR);
					inGate.x = gateXml[i].@x;
					inGate.y = gateXml[i].@y;
					inGate.label = "进城点";
					inGate.npcId = "进城点";
					
					if(gateXml[i].@standX.toString() != "")
					{
						inGate.startCircle.x = gateXml[i].@standX * 16 - inGate.x;
						inGate.startCircle.y = gateXml[i].@standY * 16 - inGate.y;
					}
				}
				else if(gateXml[i].@kind == "pre")
				{
					preGate   = new Teleport(Minimap.MINI_PRE_COLOR);
					preGate.x = gateXml[i].@x;
					preGate.y = gateXml[i].@y;
					preGate.toId = gateXml[i].@toScene;
					preGate.label = "前传送点";
					preGate.npcId = "前传送点";
					
					if(gateXml[i].@standX.toString() != "")
					{
						preGate.startCircle.x = gateXml[i].@standX * 16 - preGate.x;
						preGate.startCircle.y = gateXml[i].@standY * 16 - preGate.y;
					}
				}
				else if(gateXml[i].@kind == "next")
				{
					nextGate   = new Teleport(Minimap.MINI_NEXT_COLOR);
					nextGate.x = gateXml[i].@x;
					nextGate.y = gateXml[i].@y;
					nextGate.toId = gateXml[i].@toScene;
					
					nextGate.label = "下一个送点";
					nextGate.npcId = "下一个送点";
					if(gateXml[i].@standX.toString() != "")
					{
						nextGate.startCircle.x = gateXml[i].@standX * 16 - nextGate.x;
						nextGate.startCircle.y = gateXml[i].@standY * 16 - nextGate.y;
					}
				}
			}
			
			Common.ui.outputWindow.showMapInfo(_mapXml.scene.@id, _mapXml.scene.@name);
			Common.map.npcLayer.initLayer(npcList, inGate, preGate, nextGate);
		}
		
		//配置文件加载失败
		private function onCfgError(e:IOErrorEvent):void
		{
			Alert.showLog("加载失败: " + _mapId+".xml 文件");
		}
		
		//背景文件加载失败
		private function onBgError(e:IOErrorEvent):void
		{
			Alert.showLog("加载失败: " + _mapId+"_bg.png 文件");
		}
		
		//路径图文件加载失败
		private function onDataError(e:IOErrorEvent):void
		{
			Alert.showLog("加载失败: /res/path.png 文件");
		}
		//
	}
}
class Singleton{}